home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Over 1,000 Windows 95 Programs
/
Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso
/
1257
/
staclist.cp_
/
staclist.cp
Wrap
Text File
|
1997-04-18
|
5KB
|
294 lines
/* EasyCODE(C++) V5.1 01.03.1995 11:32:46
Object classes "list" and "stack" */
/* EasyCODE O
If=horizontal
LevelNumbers=no
LineNumbers=no
ScreenFont=System,,100,1,-13,0,700,0,0,0,0,0,0,1,2,1,34
PrinterFont=Courier,,100,2,-42,0,400,0,0,0,0,0,0,2,1,2,49
LastLevelId=32 */
/* EasyCODE ( 1
Stack/List */
#include <iostream.h>
const int Max_elem = 10;
/* EasyCODE ( 2
Declaration of Class List */
/* EasyCODE C */
class List
{
protected:
int *list; // An integer array
int nmax; // The dimension of the array
int nelem; // The number of elements
public:
/* EasyCODE ( 22
List */
/* EasyCODE F */
List(int n = Max_elem)
{
list = new int[n]; nmax = n; nelem = 0;
}
/* EasyCODE ) */
/* EasyCODE ( 21
~List */
/* EasyCODE F */
~List()
{
delete list;
}
/* EasyCODE ) */
/* EasyCODE ( 29
clear_list */
/* EasyCODE F */
void clear_list()
{
for (int i = 0; i < nmax; i++)
{
list[i]=0;
}
}
/* EasyCODE ) */
/* EasyCODE ( 5
setn */
/* EasyCODE F */
void setn(int n)
{
nelem = n;
}
/* EasyCODE ) */
/* EasyCODE ( 6
getn */
/* EasyCODE F */
int getn()
{
return nelem;
}
/* EasyCODE ) */
/* EasyCODE ( 7
incn */
/* EasyCODE F */
void incn()
{
if (nelem < nmax)
{
++nelem;
}
}
/* EasyCODE ) */
/* EasyCODE ( 8
getmax */
/* EasyCODE F */
int getmax()
{
return nmax;
}
/* EasyCODE ) */
int put_elem(int, int);
/* EasyCODE - */
int get_elem(int&, int);
/* EasyCODE - */
virtual void print();
};
/* EasyCODE E */
/* EasyCODE ) */
/* EasyCODE ( 9
Declaration of Class Stack */
/* EasyCODE C */
class Stack : public List
{
int top;
public:
/* EasyCODE ( 10
Stack */
/* EasyCODE F */
Stack()
{
top = 0;
}
/* EasyCODE ) */
/* EasyCODE ( 11
Stack */
/* EasyCODE F */
Stack(int n) : List(n)
{
top = 0;
}
/* EasyCODE ) */
int push(int elem);
/* EasyCODE - */
int pop(int& elem);
/* EasyCODE - */
void print();
};
/* EasyCODE E */
/* EasyCODE ) */
/* EasyCODE ( 19
Implementation of Class List */
/* EasyCODE ( 12
List::put_elem */
/* EasyCODE F */
int List::put_elem(int elem, int pos)
{
if (0 <= pos && pos < nmax)
{
list[pos] = elem;
return 0;
}
else
{
return -1;
}
}
/* EasyCODE ) */
/* EasyCODE ( 13
List::get_elem */
/* EasyCODE F */
int List::get_elem(int& elem, int pos)
{
if (0 <= pos && pos < nmax)
{
elem = list[pos];
return 0;
}
else
{
return -1;
}
}
/* EasyCODE ) */
/* EasyCODE ( 14
List::print */
/* EasyCODE F */
void List::print()
{
for (int i = 0; i < nelem; ++i)
{
cout << list[i] << "\n";
}
}
/* EasyCODE ) */
/* EasyCODE ) */
/* EasyCODE ( 20
Implementation of Class Stack */
/* EasyCODE ( 15
Stack::push */
/* EasyCODE F */
int Stack::push(int elem)
{
if (top < nmax)
{
list[top++] = elem;
return 0;
}
else
{
return -1;
}
}
/* EasyCODE ) */
/* EasyCODE ( 16
Stack::pop */
/* EasyCODE F */
int Stack::pop(int& elem)
{
if (top > 0)
{
elem = list[--top];
return 0;
}
else
{
return -1;
}
}
/* EasyCODE ) */
/* EasyCODE ( 17
Stack::print */
/* EasyCODE F */
void Stack::print()
{
for (int i = top-1; i >= 0; --i)
{
cout << list[i] << "\n";
}
}
/* EasyCODE ) */
/* EasyCODE ) */
/* EasyCODE ( 18
Main program */
/* EasyCODE F */
main()
{
Stack s(5);
List l, *lp;
int i = 0;
// Write numbers into the stack until stack is full
while (s.push(i+1) == 0)
{
++i;
}
// Initialize list with Null values
l.clear_list();
/* EasyCODE - */
// Write a few numbers into the list
l.put_elem(1,0);
l.put_elem(2,3);
l.put_elem(3,4);
l.put_elem(4,7);
l.put_elem(5,9);
l.setn(l.get_max());
/* EasyCODE - */
// Stack output to screen
cout << "Stack:\n";
lp = &s;
lp->print(); // Stack-print()
/* EasyCODE - */
// List output to screen
cout << "\nList:\n";
lp = &l;
lp->print(); // List-print()
}
/* EasyCODE ) */
/* EasyCODE ) */